package tgc.edu.szxt.demo.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.szxt.demo.entity.Student;
import tgc.edu.szxt.demo.entity.SysRole;
import tgc.edu.szxt.demo.entity.SysUser;
import tgc.edu.szxt.demo.entity.TbClass;
import tgc.edu.szxt.demo.service.StudentService;
import tgc.edu.szxt.demo.service.SysRoleService;
import tgc.edu.szxt.demo.service.SysUserService;
import tgc.edu.szxt.demo.service.TbClassService;
import tgc.edu.szxt.demo.web.form.Student1From;

@Controller
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private TbClassService classService;
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private SysUserService sysUserService;
	
	
	@RequestMapping("/xuesheng")
	public void xuesheng(ModelMap model){
		List<TbClass> bjs = classService.findAll();
		model.put("bjs", bjs);
	}
	
	@RequestMapping("/table")
	public String table(Integer bjId,ModelMap model){
		List<Student> Students = studentService.findByTbClassId(bjId);
		model.put("list",Students);
		return "student/table";
	}
	
	
	@RequestMapping(value = "/edit")
	public void edit(Integer bjId,ModelMap model) {
		Student stu = new Student();
		TbClass tbClass = classService.findById(bjId);
		stu.setTbClass(tbClass);
		model.put("model", stu);
	}
	
	@RequestMapping(value = "/edit2")
	public void edit2(Integer id,ModelMap model) {
		Student stu = studentService.findById(id);
		model.put("model", stu);
	}
	
	@RequestMapping(value = "/edit3")
	public void edit3(Integer id,ModelMap model) {
		Student stu = studentService.findById(id);
		model.put("model", stu);
	}
	
	@RequestMapping(value = "/save2")
	@ResponseBody
	public Object save2(Student1From form) {
		Student stu = new Student();
		Integer id = form.getId();
		stu=studentService.findById(id);
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); 
		String password = encoder.encode(form.getPassword1());
		stu.setPassword(password);
		studentService.save(stu);
		return "OK";
	}
	
	
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public Object save(Student1From form) {
		Student stu = new Student();
		Integer id = form.getId();
		if(id!=null) {
			stu = studentService.findById(id);
			BeanUtils.copyProperties(form,stu);
			stu.setUsername(form.getStuNo());
			studentService.save(stu);
			return "OK";
		}else {      
			BeanUtils.copyProperties(form,stu);
			Integer tbClassId = form.getTbClassId();
			stu.setTbClass(classService.findById(tbClassId));
			stu.setUsername(form.getStuNo());
			List<SysRole> role = new ArrayList<>();
			role.add(sysRoleService.findById(3));
			stu.setRoles(role);
			BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); 
			String password = encoder.encode(form.getPassword1());
			stu.setPassword(password);
			studentService.save(stu);
			return "OK";
		}
	}
	
	@RequestMapping(value = "/del")
	@ResponseBody
	public Object del(Integer id) {
		SysUser stu = sysUserService.findById(id);
		sysUserService.delete(stu);
		return "ok";
	}
	
}
